<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/jquery.min.js"></script>
    <script src="./js/jquery.form.js"></script>
    <style>
        table,table tr th, table tr td { border:1px solid #0094ff; }
        table { text-align: center; border-collapse: collapse;}
    </style>
</head>
<body>
<table class="table" style="border: 1px;">
    <thead>
    <tr>
        <th class="center">市（州）名称</th>
        <th class="center">县(市、区)名称</th>
        <th class="center">计划指标(户)</th>
        <th class="center">乡镇名称</th>
        <th class="center">村名称</th>
        <th class="center">分解到村户数（个）</th>
        <th class="center">已开工户数（个）</th>
        <th class="center">已竣工户数（个）</th>
        <th class="center">已验收户数（个）</th>
        <th class="center">资金发放户数（个）</th>
    </tr>
    </thead>
    <tbody id="tbody"></tbody>
</table>
<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $.ajax({
            type: 'GET',
            url: "http://172.18.84.213:8080/rest/reports/0/date/2018-11-11/detail",
            data : "radom=" + Math.random(),
            dataType: 'json',
            success: function( result ) {
                if (result.success) {
                    var s = setDetailData(result.data);
                    $("#tbody").html(s);
                }
                else {
                    alert(result.message);
                    if (result.logged) {
                        outLogin();
                    }
                }
            }
        });

        function setCun(fatherId, nodes, father) {
            if (father.sum == undefined) {
                father.sum = 0;
            }

            if (nodes != undefined) {
                var r = "";
                for (var i = 0; i < nodes.length; i ++) {
                    var o = nodes[i];
                    if (i != 0) {
                        r += "<tr>";
                    }

                    if (o.id.substr(6, 2) == "00") {
                        r += "<td></td>";
                    }
                    r += "<td>" + o.name + "</td><td>" + o.u + "</td><td>" + o.b + "</td><td>" + o.e + "</td><td>" + o.a + "</td><td>" + o.f + "</td></tr>";
                }
                father.sum += nodes.length;
                return r;
            }

            father.sum = 1;
            return "<td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>";
        }
        function setZhen(fatherId, nodes, father) {
            if (father.sum == undefined) {
                father.sum = 0;
            }

            if (nodes != undefined) {
                var r = "";
                for (var i = 0; i < nodes.length; i ++) {
                    var o = nodes[i];
                    var s = setCun(o.id, o.nodes, o);
                    if (i != 0) {
                        r += "<tr>";
                    }
                    if (o.sum > 0) {
                        r += "<td rowspan='" + o.sum + "'>" + o.name + "</td>" + s;
                    }
                    else {
                        r += "<td>" + o.name + "</td>" + s;
                    }
                    father.sum += o.sum;

                }
                return r;
            }

            father.sum = 1;
            return "<td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>";
        }

        function setQu(fatherId, nodes, father) {
            if (father.sum == undefined) {
                father.sum = 0;
            }

            if (nodes != undefined) {
                var r = "";
                for (var i = 0; i < nodes.length; i++) {
                    var o = nodes[i];
                    if (o.id == fatherId) {

                        if (o.nodes != undefined && o.nodes.length > 0) {
                            r = setQu(o.id, o.nodes, o);
                        }
                        else {
                            o.sum = 1;
                            r = "<td>" + o.name + "</td><td>" + o.n + "</td><td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>";
                        }
                    }
                    else {
                        var s;
                        if (o.nodes != undefined && o.nodes.length > 0 && o.nodes[0].id.substr(6, 2) == "00") {
                            s = setCun(o.id, o.nodes, o);
                        }
                        else {
                            s = setZhen(o.id, o.nodes, o);
                        }
                        if (i != 0) {
                            r += "<tr>";
                        }
                        if (o.sum > 0) {
                            r += "<td rowspan='" + o.sum + "'>" + o.name + "</td><td rowspan='" + o.sum + "'>" + o.n + "</td>" + s;
                        }
                        else {
                            r += "<td>" + o.name + "</td><td>" + o.n + "</td>" + s;
                        }
                    }
                    father.sum += o.sum;
                }
                return r;
            }

            father.sum = 1;
            return "<td></td><td></td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>";
        }
        function setDetailData(data) {
            var r = "";

            for (var i = 0; i < data.length; i ++) {
                var o = data[i];
                var ss = "";
                if (o.id == "0111" || o.id == "0112") {
                    var s = setZhen(o.id, o.nodes[0].nodes, o);
                    ss += "<tr>";

                    if (o.sum > 0) {
                        ss += "<td rowspan='" + o.sum + "'>" + o.name + "</td><td rowspan='" + o.sum + "'></td><td rowspan='" + o.sum + "'>" + o.nodes[0].n + "</td>" + s;
                    }
                    else {
                        ss += "<td>" + o.name + "</td>" + s;
                    }

                }
                else {
                    var s = setQu(o.id, o.nodes, o);
                    ss += "<tr>";

                    if (o.sum > 0) {
                        ss += "<td rowspan='" + o.sum + "'>" + o.name + "</td>" + s;
                    }
                    else {
                        ss += "<td>" + o.name + "</td>" + s;
                    }
                }

                r += ss;
            }

            return r;
        }
    });

</script>
</body>
</html>